
package com.dongdongshop.controller;

import cn.hutool.json.JSONUtil;
import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.CartService;
import com.dongdongshop.utils.CookieUtils;
import com.dongdongshop.vo.CartVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RequestMapping("/cart")
@Controller
public class CartController {

    @DubboReference
    private CartService cartService;

    //cookie使用的注意事项:必须使用域名,127...不可以 或者使用 localhost
    @RequestMapping("/addCart")
    @ResponseBody
    public Result addCart(HttpServletRequest request, HttpServletResponse response, Long itemId, Integer num){
        //获取购物车之前,获取用户的登录状态
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();

        //1 在加入购物车之前我们要先获取cookie购物车之前的信息,
        Result<List<CartVo>> result = getCart(request,response);
        List<CartVo> cartlist = result.getData();
        //2 将获取到的数据存入 我们的购物车容器中
        cartlist = cartService.selectItemById(cartlist,itemId,num);
        //3 然后将最后的数据存入Cookie或redis中
        String s = JSONUtil.toJsonStr(cartlist);
        if (user == null){
            CookieUtils.setCookie(request,response,"cartList",s,true);
        }else {
            //将数据存入redis
            cartService.addCartListToRedis(user.getId(),cartlist);
        }

        return result.OK();
    }

    //获取购物车信息
    @RequestMapping("/getCart")
    @ResponseBody
    public Result<List<CartVo>> getCart(HttpServletRequest request,HttpServletResponse response){
        //获取user
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        if (user == null){

            List<CartVo> cartListCookie = getCookie(request);
            return Result.OK().setData(cartListCookie);
        }else {//需要将cookie中的数据和redis中的数据合并

            //从cookie中获取数据
            List<CartVo> cartListCookie = getCookie(request);
            //从redis中获取数据
            List<CartVo> cartListRedis = cartService.getCartListFormRedis(user.getId());
            if (cartListCookie.size() != 0){

                //将Cookie中的数据添加到Redis中
                List<CartVo> cartListCookieAndRedis = cartService.getCookieAndRedis(cartListCookie,cartListRedis);

                //然后将Cookie中的数据删除
                delCookie(request,response);

                return Result.OK().setData(cartListCookieAndRedis);
            }
            //返回合并后的数据
            return Result.OK().setData(cartListRedis);
        }
    }



    @RequestMapping("/delCookie")
    @ResponseBody
    private String delCookie(HttpServletRequest request, HttpServletResponse response){
        CookieUtils.deleteCookie(request,response,"cartList");
        return "ok";
    }

    private List<CartVo> getCookie(HttpServletRequest request){
        //获取Cookie里面的数据,为json格式
        String cartList = CookieUtils.getCookieValue(request, "cartList", true);
//        判断cartList是否为空,避免转成json格式失败
        if (cartList == null || cartList.equals("")){
            cartList = "[]";
        }
//        将字符串转换成集合
        List<CartVo> cartVos = JSONUtil.toList(cartList, CartVo.class);
        return cartVos;
    }

    @RequestMapping("/toCart")
    public String toCart(){
        return "cart";
    }
}
